package com.uxhao.oss.controller;

import com.uxhao.commonutils.R;
import com.uxhao.oss.service.OssService;
import com.uxhao.oss.utils.ConstantPropertiesUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传文件
 *
 * @author uxhao
 * @date 2021/1/9 21:15
 */
@Api(description = "Oss文件上传管理")
//@CrossOrigin
@RestController
@RequestMapping("/eduoss/fileoss")
public class OssController {

    @Autowired
    private OssService ossService;

    //上传头像的方法
    @ApiOperation("上传头像")
    @PostMapping
    public R uploadOssFile(@ApiParam(name = "file", value = "文件", required = true)
                           @RequestParam("file") MultipartFile file,
                           @ApiParam(name = "host",value = "文件上传路径", required = false)
                           @RequestParam(value = "host", required = false) String host) {
        if(!StringUtils.isEmpty(host)){
            ConstantPropertiesUtils.FILE_HOST = host;
        }
        String url = ossService.uploadFileAvatar(file);
        return R.ok().data("url", url);
    }
}
